package cn.e2win.biz.user;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.record.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysRole;
import cn.e2win.base.entity.SysUser;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.base.json.vo.Result;
import cn.e2win.biz.annotation.PageParam;
import cn.e2win.biz.base.BaseController;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.util.StringUtils;


@Controller
@Scope("request")
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	
	@Override
	public BaseService initService() {
		return roleService;
	}


	@RequestMapping("toEditRole/{id}")
	public String toEditRole(HttpServletRequest request, @PathVariable(ID) Integer id, ModelMap modelMap){
		SysRole obj = (SysRole)roleService.getEntity(SysRole.class, id);
		modelMap.put(OBJECT, obj);
		roleService.getMenuTree(id, modelMap);
		return "user/roleEdit";
	}
	@RequestMapping("editRole")
	@ResponseBody
	public Result editRole(HttpServletRequest request,@ModelAttribute SysRole obj,ModelMap modelMap) throws MyException{
		Result r = new Result();

		String tempIds = request.getParameter("ids");
		String temp = "-999";
		for(String tempId : tempIds.split("\\,")){
			if(StringUtils.isNumeric(tempId)){
				temp = temp + "," + tempId;
			}
			
		}
		roleService.editRole(obj, temp.split("\\,"));
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		return r;
	}
	@RequestMapping("delete/{id}")
	@ResponseBody
	public Result delete(HttpServletRequest request,@PathVariable(ID) Integer id, ModelMap modelMap){
		Result r = new Result();
	    roleService.delEntity(SysRole.class, id);
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		return r;
	}

	@RequestMapping("rolePager")
	public String rolePager(HttpServletRequest request,  ModelMap modelMap){
		if(pager.getCurrUser().getId()>0){
			pager.getScMap().put("id_B", "1");
		}
		roleService.pagerRole(pager);
		modelMap.put("pager", pager);
		String[] path = request.getServletPath().split("/");
		String module = path[1];
		String entity = path[2];
		modelMap.put(MODULE, module);
		modelMap.put(ENTITY, entity);
		return "user/rolePager";
	}
	@RequestMapping("toAssign/{id}")
	public String toAssign(HttpServletRequest request, @PathVariable(ID) Integer id, ModelMap modelMap){
		try {
			roleService.getMenuTree(id, modelMap);
			return "user/roleAssign";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return "msg/common/fail";
		}
	}
	
/*	@RequestMapping("editRoleFunction")
	@ResponseBody
	public Result editRoleFunction(HttpServletRequest request,@RequestParam(ID) Integer id){
		String ids = request.getParameter("ids");
		String[] idArr = ids.split("\\,");
		roleService.assignFuntions(id, idArr);
		return new Result();
	}*/
}
